﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        private int id;
        public Form4(int id, string title, string author)
        {  
            this.id = id;
            textBox1.Text = title;
            textBox1.Text = author;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    var sql = string.Format("update Artycles set Title='{0}',Author='{1}', where Id={2}", textBox1.Text, textBox1.Text, this.id);

                    var res = DbHelper.AddOrAddOrEditOrDeleteSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("更新成功！","提示");

                        this.DialogResult = DialogResult.Yes;

                        this.Close();
                    }
                }

            }
            else
            {
                var title = textBox1.Text;
                var author = textBox2.Text;

                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Artycles (Title,Author) values ('{0}','{1}')", title, author);

                    var res = DbHelper.AddOrAddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！");

                        this.DialogResult = DialogResult.Yes;

                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败。");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("文章名称不能为空");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        { 
            //取消
            this.Close();
        }
    }
}
